/**
* mainsvr 定义
* @author thinkry
*/

#ifndef __MAINSVR_DEF_H__
#define __MAINSVR_DEF_H__

//#define WIN32 1

#include "CTBase.h"
#include "CTBuffer.h"
#include "CTStat.h"
#include "CTSizeString.h"
#include "CTArray.h"
#include "CTObjectMgr.h"
#include "CTHashObjectMgr.h"
#include "CTStandardTimerMgr.h"
#include "CTShmQueue.h"
#include "net/CTEpoll.h"
#include "CTMsgBase.h"
#include "CTCodeConvert.h"
#include "CTConfig.h"
#include "CTServerUtils.h"
#include "CTAsyncCmd.h"
#include "CTAsyncCmdFactory.h"
#include "base/CTFile.h"
#include "base/CTBaseDefine.h"

#include "commerrno.h"
#include "CSMsgDef.h"
#include "PBMsgHead.pb.h"

using namespace CTLib;

//===============================================
// 相关文件的定义，做为一个约定，不用在配置文件中读取

#define APPNAME             "mainsvr"
#define FILE_CONFIG         "../config/" APPNAME ".conf"
#define FILE_NORMALLOG      "../log/" APPNAME ".log"
#define FILE_WARLOG         "../log/" APPNAME ".wlg"
#define FILE_ERRORLOG       "../log/" APPNAME ".err"
#define FILE_STATLOG        "../log/" APPNAME ".sta"
#define FILE_PIDFILE        "../bin/" APPNAME ".pid"
#define FILE_UPDATE         "../log/" APPNAME ".upt"
#define FILE_BATTLE         "../log/" APPNAME ".bat"
#define FILE_MESSAGE        "../log/" APPNAME ".msg"
#define BILL_PATH           "../bill/"
#define BILL_SUFFIX         ".bil"

//===============================================
// 这两个定义是为了能够在Windows下编译
#ifdef WIN32
    #define signal(signum, handler) (0)
    #define getopt(argc, argv, optstring) (0)

    #define SIGUSR1 10
    #define SIGUSR2 12
#else
    #include <sys/types.h>
    #include <signal.h>
#endif

//===============================================

enum main_const
{
    MAIN_EVENT_SIZE =		64, ///< epoll参数
    MAIN_WAIT_SIZE  =		64, ///< epoll参数
    MAX_IPLEN = 32,
};

/**
* 异步命令的Do参数（PB版本）
*/
class CCmdPBDoParam
{
public:
    void*               m_pMain;
    CEnpNetHead*        m_pstEnpHead;
    PBMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdPBDoParam(void* pMain, CEnpNetHead& stEnpHead, PBMsgHead& stMsgHead, CTCodeStream& stCode) :
    m_pMain(pMain),
        m_pstEnpHead(&stEnpHead),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {
        CT_ASSERT(m_pMain);
    }

private:
    // 禁止赋值
    CCmdPBDoParam& operator = (const CCmdPBDoParam& obj)
    {
        return *this;
    }
};

/**
* 异步命令的Do参数
*/
class CCmdDoParam
{
public:
    void*               m_pMain;
    CEnpNetHead*        m_pstEnpHead;
    CTMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdDoParam(void* pMain, CEnpNetHead& stEnpHead, CTMsgHead& stMsgHead, CTCodeStream& stCode) :
        m_pMain(pMain),
        m_pstEnpHead(&stEnpHead),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {
        CT_ASSERT(m_pMain);
    }

private:
    // 禁止赋值
    CCmdDoParam& operator = (const CCmdDoParam& obj)
    {
        return *this;
    }
};

/**
* 异步命令的Answer参数
*/
class CCmdOnAnswerParam
{
public:
    void*               m_pMain;
    CTMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdOnAnswerParam(void* pMain, CTMsgHead& stMsgHead, CTCodeStream& stCode) :
        m_pMain(pMain),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {
        CT_ASSERT(m_pMain);
    }

private:
    // 禁止赋值
    CCmdOnAnswerParam& operator = (const CCmdOnAnswerParam& obj)
    {
        return *this;
    }
};

typedef int (*PCodeDate)(void* pData, CTCodeStream& s);

//===============================================
// 统计项的定义

// 统计的CTLog
#define CT_STAT 	(CTLib::CTSingleton<CTLib::CTLog, 2>::Instance())
#define CT_WLOG		(CTLib::CTSingleton<CTLib::CTLog, 3>::Instance())
#define CT_UPT   	(CTLib::CTSingleton<CTLib::CTLog, 4>::Instance())
#define CT_BAT   	(CTLib::CTSingleton<CTLib::CTLog, 5>::Instance())
#define CT_MSG   	(CTLib::CTSingleton<CTLib::CTLog, 6>::Instance())
#define CT_BIL   	(CTLib::CTSingleton<CTLib::CTLog, 7>::Instance())

//账单
#define CT_BILL(x) do { CT_BIL->LogInfo x; } while(0)

#define CT_MESSAGE(x) do { CT_MSG->LogDebug x; } while(0)

//更新日志
#ifndef _DEBUG
#define CT_UPDATE(x) do { \
    ; } while(0)
#define CT_BATTLE(x) while(0) { \
    CT_BAT->LogTrace x; } 
#else
#define CT_UPDATE(x) do { \
    CT_UPT->LogTrace x; } while(0)
#define CT_BATTLE(x) do { \
    if (CT_BAT->GetFormat() & CTLib::EFileLine) { \
    CT_BAT->m_pFile = __FILE__; \
    CT_BAT->m_uiLine = __LINE__; } \
    CT_BAT->LogTrace x; } while(0)
#endif

/**
* 统计项.
* 必须以0开始顺序编号, 方便和描述的下标对应
*/
enum EMainStat
{
    main_stat_recvmsg = 0,
    main_stat_sendmsg,
    main_stat_accept,
    main_stat_idleclose,
    main_stat_mainloop,
    main_stat_waittimeout,
    main_stat_check,
	main_stat_async_cmds,
    main_stat_timeout_cmds,
};


#endif //__MAINSVR_DEF_H__
